#include "./hovertrigger.h"
#include "./uiobject.h"

using namespace MiniUI;

HoverTrigger::HoverTrigger()
: m_pObj(nullptr)
, m_pTimer(nullptr)
{
    m_pTimer = Timer::CreateInstance();
    m_pTimer->SetInterval(200);
    m_pTimer->OnTimer += EventObject<OnNotifyEventFunc>(this, &HoverTrigger::TimerEvent);
}

HoverTrigger::~HoverTrigger()
{
    if (m_pTimer)
    {
        m_pTimer->Release();
    }
}

void HoverTrigger::Bind(UIObject* pObj)
{
    if (m_pObj != pObj)
    {
        if (m_pObj)
        {
            m_pObj->Release();
        }
        if (pObj)
        {
            pObj->AddRef();
        }
        m_pObj = pObj;
    }
    if (m_pObj)
    {
        if (m_pTimer)
        {
            m_pTimer->Stop();
            m_pTimer->Start();
        }
    }
}

void HoverTrigger::Unbind(UIObject* pObj)
{
    if (pObj)
    {
        if (m_pObj == pObj)
        {
            m_pObj->Release();
            m_pObj = nullptr;
            if (m_pTimer)
            {
                m_pTimer->Stop();
            }
        }
    }
}

void HoverTrigger::TimerEvent(const Object* sender)
{
    if (m_pObj)
    {
        m_pObj->DoMouseHover();
    }
    m_pTimer->Stop();
}